﻿Imports System.IO

Public Class frmMain
	Public Sub New()
		InitializeComponent()
		Text = My.Application.Info.ProductName
		notifyIco.Text = Text
		Icon = My.Resources.ico_key
		notifyIco.Icon = Icon
		Left = My.Computer.Screen.WorkingArea.Right - Width
		Top = My.Computer.Screen.WorkingArea.Bottom - Height
		tmrMain.Enabled = True
	End Sub

	Private Sub notifyIco_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles notifyIco.DoubleClick
		ShowHideForm()
	End Sub

	Private Sub ShowHideForm()
		If WindowState = FormWindowState.Normal Then
			WindowState = FormWindowState.Minimized
		Else
			WindowState = FormWindowState.Normal
		End If
		RestoreLayout()
	End Sub

	Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
		e.Cancel = MessageBox.Show(Me, "Are you sure to close " + _
		   My.Application.Info.ProductName + "?", "Please confirm", _
		MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, _
		MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No
		MyBase.OnClosing(e)
	End Sub

	Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
		MyBase.OnSizeChanged(e)
		If Not Created Or IsDisposed Or Disposing Then Exit Sub
		RestoreLayout()
	End Sub

	Private Sub RestoreLayout()
		ShowInTaskbar = WindowState <> FormWindowState.Minimized
		Visible = ShowInTaskbar

		If Visible = False Then
			ctxtShowHide.Text = "Show"
			ctxtShowHide.ToolTipText = "Show Settings Form"
			Exit Sub
		Else
			ctxtShowHide.Text = "Hide"
			ctxtShowHide.ToolTipText = "Hide Settings Form"
		End If

		Activate()
		BringToFront()
		Focus()
		PerformLayout()
	End Sub

	Private Sub dlgOpenTemplateFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpenTemplateFile.FileOk
		OpenTemplateFileAsync()
	End Sub

	Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
		MyBase.OnLoad(e)

		clsNotificationBalloon.SetNotificationIcon(notifyIco)
		AddHandler My.Application.Manager.FileLoaded, AddressOf OnFileLoaded
		AddHandler My.Application.Manager.FileSaved, AddressOf OnFileSaved
		My.Application.Manager.SetTimer(tmrMain)

		dlgOpenTemplateFile.FileName = My.Settings.LastTemplateFile
		dlgSaveTemplateFile.FileName = dlgOpenTemplateFile.FileName
		OpenTemplateFileAsync()
	End Sub

	Private Sub OnFileSaved(ByVal sender As Object, ByVal e As EventArgs)

	End Sub

	Private Sub OnFileLoaded(ByVal sender As Object, ByVal e As EventArgs)
		UpdateListBoxDataSource()
	End Sub

	Private Sub OpenTemplateFileAsync()
		If Created And Disposing = False And IsDisposed = False Then
			Dim m As New MethodInvoker(AddressOf OpenTemplateFile)
			BeginInvoke(m)
		Else
			OpenTemplateFile()
		End If
	End Sub

	Private Sub OpenTemplateFile()
		UseWaitCursor = True
		Try
			If File.Exists(dlgOpenTemplateFile.FileName) Then
				If My.Application.Manager.LoadFromFile(dlgOpenTemplateFile.FileName) Then
					Text = My.Application.Info.ProductName + " (" + dlgOpenTemplateFile.FileName + ")"
					My.Settings.LastTemplateFile = dlgOpenTemplateFile.FileName
					My.Settings.Save()
				End If
			End If
		Catch ex As Exception
			MessageBox.Show(Me, ex.Message.Trim(), "Error(s) while reading template file", _
				   MessageBoxButtons.OK, MessageBoxIcon.Error)
			modCommon.RegisterException(ex)
		End Try
		UseWaitCursor = False
	End Sub

	Private Sub dlgSaveTemplateFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgSaveTemplateFile.FileOk
		My.Application.Manager.CurrentFilePath = dlgSaveTemplateFile.FileName
		SaveTemplateFileAsync()
	End Sub

	Private Sub SaveTemplateFileAsync()
		If Created And Disposing = False And IsDisposed = False Then
			Dim m As New MethodInvoker(AddressOf SaveTemplateFile)
			BeginInvoke(m)
		Else
			SaveTemplateFile()
		End If
	End Sub

	Private Sub SaveTemplateFile()
		UseWaitCursor = True
		For Each item In lstItems.Items
			If My.Application.Manager.NotificationItems.Items.Contains(item) Then Continue For
			My.Application.Manager.NotificationItems.Items.Add(item)
		Next
		If My.Application.Manager.SaveToFile(My.Application.Manager.CurrentFilePath) Then
			Text = My.Application.Info.ProductName + " (" + My.Application.Manager.CurrentFilePath + ")"
			My.Settings.LastTemplateFile = My.Application.Manager.CurrentFilePath
			My.Settings.Save()
		End If
		UseWaitCursor = False
	End Sub

	Private Sub tlOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlOpenFile.Click
		dlgOpenTemplateFile.ShowDialog(Me)
	End Sub

	Private Sub tlSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlSaveFile.ButtonClick
		If Text = My.Application.Info.ProductName Then
			dlgSaveTemplateFile.ShowDialog(Me)
		Else
			SaveTemplateFileAsync()
		End If
	End Sub

	Private Sub tlAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlAddItem.Click
		Dim item As New clsNotification("New Item (" & (lstItems.Items.Count + 1) & ")")
		'If lstItems.SelectedIndex < 0 Then
		'	My.Application.Manager.AddItem(item)
		'Else
		'	My.Application.Manager.NotificationItems.Items.Insert(lstItems.SelectedIndex + 1, item)
		'End If
		'UpdateListBoxDataSource()
		'lstItems.SelectedItem = item

		itemsBindingSource.Insert(lstItems.SelectedIndex + 1, item)
		lstItems.SelectedItem = item
	End Sub

	Private Sub tlRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlRemoveItem.Click
		If lstItems.SelectedItem Is Nothing Then Exit Sub
		'My.Application.Manager.NotificationItems.Items.Remove(lstItems.SelectedItem)
		'UpdateListBoxDataSource()

		Dim idx = itemsBindingSource.IndexOf(lstItems.SelectedItem)
		itemsBindingSource.Remove(lstItems.SelectedItem)

		If idx > 0 Then
			lstItems.SelectedIndex = idx - 1
		ElseIf idx < lstItems.Items.Count - 1 Then
			lstItems.SelectedIndex = idx + 1
		End If
	End Sub

	Private Sub tlRemoveAllItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlRemoveAllItems.Click
		'My.Application.Manager.NotificationItems.Items.Clear()
		'UpdateListBoxDataSource()
		itemsBindingSource.Clear()
	End Sub

	Private Sub tlMoveItemUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlMoveItemUp.Click
		MoveItem(True)
	End Sub

	Private Sub MoveItem(ByVal booUp As Boolean)
		If (booUp And lstItems.SelectedIndex < 1) Or booUp = False And _
		(lstItems.SelectedIndex > lstItems.Items.Count - 2) Then Exit Sub

		Dim diff As Integer = 1
		If booUp Then diff = -1

		'Dim item1 = My.Application.Manager.NotificationItems.Items(lstItems.SelectedIndex)
		'Dim item2 = My.Application.Manager.NotificationItems.Items(lstItems.SelectedIndex + diff)

		'My.Application.Manager.NotificationItems.Items(lstItems.SelectedIndex + diff) = item1
		'My.Application.Manager.NotificationItems.Items(lstItems.SelectedIndex) = item2

		'UpdateListBoxDataSource()
		'lstItems.SelectedItem = item1

		Dim item1 = itemsBindingSource.Item(lstItems.SelectedIndex)
		Dim item2 = itemsBindingSource.Item(lstItems.SelectedIndex + diff)

		itemsBindingSource.Item(lstItems.SelectedIndex + diff) = item1
		itemsBindingSource.Item(lstItems.SelectedIndex) = item2

		lstItems.SelectedItem = item1
	End Sub

	Private Sub UpdateListBoxDataSource()
		Dim selected = lstItems.SelectedItem

		'lstItems.DataSource = Nothing
		'lstItems.DataSource = My.Application.Manager.NotificationItems.Items

		itemsBindingSource.EndEdit()
		'itemsBindingSource.SuspendBinding()

		itemsBindingSource.DataSource = My.Application.Manager.NotificationItems.Items
		'itemsBindingSource.ResumeBinding()

		lstItems.SelectedIndex = -1

		If selected Is Nothing Then
			If lstItems.Items.Count > 0 Then lstItems.SelectedIndex = 0
			Exit Sub
		End If
		If lstItems.Items.Contains(selected) = False Then
			If lstItems.Items.Count > 0 Then lstItems.SelectedIndex = 0
			Exit Sub
		End If

		lstItems.SelectedItem = selected
	End Sub

	Private Sub tlMoveItemDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlMoveItemDown.Click
		MoveItem(False)
	End Sub

	Private Sub ctxtAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxtAbout.Click
		ShowAboutInfoUI()
	End Sub

	Private Sub ctxtExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxtExit.Click
		Close()
	End Sub

	Private Sub ctxtShowHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxtShowHide.Click
		ShowHideForm()
	End Sub

	Private Sub tlSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlSaveAs.Click
		dlgSaveTemplateFile.ShowDialog(Me)
	End Sub

	Private Sub tlNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlNew.Click
		SetNew()
	End Sub

	Private Sub SetNew()
		lstItems.SelectedItem = Nothing
		dlgOpenTemplateFile.FileName = String.Empty
		dlgSaveTemplateFile.FileName = String.Empty
		Text = My.Application.Info.ProductName
		itemsBindingSource.DataSource = Nothing
		itemsBindingSource.Clear()
		ucNotificationSetup.SetDataSource(Nothing)
	End Sub

	Private Sub tlAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlAbout.Click
		modCommon.ShowAboutInfoUI()
	End Sub

	Private Sub lstItems_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstItems.SelectedIndexChanged
		'If lstItems.SelectedItem Is Nothing Then
		'	tlItemName.Clear()
		'Else
		'	UpdateItemNameTextBox(lstItems.SelectedItem)
		'End If
		'ucNotificationSetup.SetDataSource(lstItems.SelectedItem)
	End Sub

	Private Sub UpdateItemNameTextBox(ByVal item As clsNotification)
		If item Is Nothing Then Exit Sub
		If item.Name Is Nothing Then item.Name = String.Empty
		tlItemName.Text = item.Name
	End Sub

	Private Sub UpdateItemNameFromTextBox()
		If lstItems.SelectedItem Is Nothing Then Exit Sub
		Dim item As clsNotification = lstItems.SelectedItem
		If item Is Nothing Then Exit Sub
		item.Name = tlItemName.Text
		itemsBindingSource.ResetItem(lstItems.SelectedIndex)
		'UpdateListBoxDataSource()
	End Sub

	Private Sub tlItemName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tlItemName.KeyDown
		If e.KeyCode = Keys.Enter Then UpdateItemNameFromTextBox()
	End Sub

	Private Sub tlItemName_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles tlItemName.Validated
		UpdateItemNameFromTextBox()
	End Sub

	Private Sub notifyIco_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles notifyIco.MouseDown
		If e Is Nothing Or Visible = False Then Exit Sub
		If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
		Activate()
		BringToFront()
	End Sub

	Private prevData As Object

	Private Sub itemsBindingSource_CurrentItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles itemsBindingSource.CurrentItemChanged
		If lstItems.SelectedItem Is Nothing Then
			tlItemName.Clear()
		Else
			UpdateItemNameTextBox(lstItems.SelectedItem)
		End If
		If Equals(prevData, lstItems.SelectedItem) Then Exit Sub
		ucNotificationSetup.SetDataSource(lstItems.SelectedItem)
	End Sub

	Private Sub tlExportBugsLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlExportBugsLog.Click
		UseWaitCursor = True
		modCommon.ExportErrorLog()
		UseWaitCursor = False
	End Sub
End Class